package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.ArticleApi;
import com.jmp.base.dto.*;
import com.jmp.feign.GoodsFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.Goods;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.GoodsBaseService;
import one.stand.service.OrderInfoBaseService;
import one.stand.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api(description = "拼团管理")
@RestController
@RequestMapping("adminApi/goods/group")
@Slf4j
public class GoodsGroupController {
    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private OrderInfoBaseService orderInfoBaseService;
    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;

    @ApiOperation("列表")
    @GetMapping("list")
    @SecurityAuthCheck
    public ResultModel<GoodsGroupListResponse> list(PageRequest request) {

        GoodsGroupListDto goodsGroupListDto = GoodsGroupListDto.builder()
                .companyId(CompanyBaseService.DEFAULT_COMPANY_ID)
                .build();
        goodsGroupListDto.setPageInit(request.getPageNum(), request.getPageSize());
        ResultModel<GoodsGroupListVo> resultModel = goodsFeign.groupList(goodsGroupListDto);
        AssertUtil.isTrue(resultModel);
        PageResponse2<GoodsGroupListResponse.GoodsGroup> page = ConvertUtils.page2(resultModel.getData().getPage(), vo -> {
            Goods goods = goodsBaseService.get(vo.getGoodsId());
            return GoodsGroupListResponse.GoodsGroup.builder()
                    .id(vo.getId())
                    .title(vo.getTitle())
                    .photo(vo.getPhoto())
                    .startTime(Objects.nonNull(vo.getStartTime()) ? vo.getStartTime().getTime() : null)
                    .endTime(Objects.nonNull(vo.getEndTime()) ? vo.getEndTime().getTime() : null)
                    .status(vo.getStatus())
                    .orderNum(orderInfoBaseService.countGroup(vo.getId()))
                    .orderUnSendNum(orderInfoBaseService.countGroupWaitDeliver(vo.getId()))
                    .retailPrice(goods.getRetailPrice())
                    .wholesalePrice(goods.getWholesalePrice())
                    .groupBuyingPrice(goods.getPrice())
                    .build();
        });

        PageHelper.startPage(1, 100);// 100 标签应该不会超过100
        List<CompanyCustomerLable> labels = companyApiAdminMapper.selectLabel(ArticleApi.DEFAULT_COMPANYID, null);
        List<GoodsGroupListResponse.Label> labelList = ConvertUtils.list(labels, label -> GoodsGroupListResponse.Label.builder()
                .id(label.getLableId())
                .name(label.getLableName())
                .build());

        GoodsGroupListResponse response = GoodsGroupListResponse.builder()
                .page(page)
                .labelList(labelList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("详情")
    @GetMapping("info")
    @SecurityAuthCheck
    public ResultModel<GoodsGroupInfoResponse> info(GoodsGroupInfoRequest request) {

        ResultModel<GoodsGroupInfoVo> resultModel = goodsFeign.groupInfo(GoodsGroupInfoDto.builder().id(request.getId()).build());
        AssertUtil.isTrue(resultModel);
        GoodsGroupInfoVo vo = resultModel.getData();

        GoodsGroupInfoResponse response = GoodsGroupInfoResponse.builder()
                .id(vo.getId())
                .title(vo.getTitle())
                .photo(vo.getPhoto())
                .hidePhotos(vo.getHidePhotos())
                .videoUrl(vo.getVideoUrl())
                .goodsId(vo.getGoodsId())
                .endTime(Objects.nonNull(vo.getEndTime()) ? vo.getEndTime().getTime() : null)
                .build();
        return ResultModel.success(response);
    }

    @PostMapping("save")
    @SecurityAuthCheck
    public ResultModel save(@RequestBody GoodsGroupSaveRequest request) {

        ResultModel resultModel = goodsFeign.groupSave(GoodsGroupSaveDto.builder()
                .id(request.getId())
                .title(request.getTitle())
                .photo(request.getPhoto())
                .hidePhotos(request.getHidePhotos())
                .videoUrl(request.getVideoUrl())
                .goodsId(request.getGoodsId())
                .endTime(request.getEndTime())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @PostMapping("up")
    @SecurityAuthCheck
    public ResultModel up(@RequestBody GoodsGroupUpRequest request) {

        ResultModel resultModel = goodsFeign.groupUp(GoodsGroupUpDto.builder()
                .id(request.getId())
                .labelIds(request.getLabelIds())
                .pushTitle(request.getPushTitle())
                .pushContent(request.getPushContent())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @PostMapping("down")
    @SecurityAuthCheck
    public ResultModel down(@RequestBody GoodsGroupInfoRequest request) {

        ResultModel resultModel = goodsFeign.groupDown(GoodsGroupInfoDto.builder()
                .id(request.getId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @PostMapping("remove")
    @SecurityAuthCheck
    public ResultModel remove(@RequestBody GoodsGroupInfoRequest request) {

        ResultModel resultModel = goodsFeign.groupRemove(GoodsGroupInfoDto.builder()
                .id(request.getId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    @Data
    public static class GoodsGroupSaveRequest {
        private Integer id;
        private String title;
        private String photo;
        private String hidePhotos;
        private String videoUrl;
        private Integer goodsId;
        private Date endTime;
    }

    @Data
    @Builder
    public static class GoodsGroupListResponse {
        private PageResponse2<GoodsGroup> page;
        private List<Label> labelList;

        @Data
        @Builder
        public static class GoodsGroup {
            private Integer id;
            private String title;
            private String photo;
            private Integer status;

            private BigDecimal retailPrice;
            private BigDecimal wholesalePrice;
            private BigDecimal groupBuyingPrice;
            private Long startTime;
            private Long endTime;
            private Integer orderNum;
            private Integer orderUnSendNum;
        }

        @Data
        @Builder
        public static class Label {
            private Integer id;
            private String name;
        }
    }

    @Data
    public static class GoodsGroupInfoRequest {
        private Integer id;
    }

    @Data
    @Builder
    public static class GoodsGroupInfoResponse {
        private Integer id;
        private String title;
        private String photo;
        private String hidePhotos;
        private String videoUrl;
        private Integer goodsId;
        private Long endTime;
    }

    @Data
    public static class GoodsGroupUpRequest {
        private Integer id;
        private List<Integer> labelIds;
        private String pushTitle;
        private String pushContent;
    }
}
